package org.compiere.apps.search;

import java.awt.Frame;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.swing.JLabel;

import org.adempiere.plaf.AdempierePLAF;
import org.compiere.apps.ALayout;
import org.compiere.grid.ed.VDate;
import org.compiere.minigrid.IDColumn;
import org.compiere.swing.CLabel;
import org.compiere.swing.CTextField;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.Msg;

public class InfoHRMOrder extends Info
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8843984489446434709L;
	
	String orderType = "";
	
	/**
	 *  Detail Protected Constructor
	 *  @param frame parent frame
	 *  @param modal modal
	 *  @param WindowNo window no
	 *  @param value query value
	 *  @param multiSelection multiple selections
	 *  @param whereClause where clause
	 */
	protected InfoHRMOrder(Frame frame, boolean modal, int WindowNo,
							String tableName, String value, boolean multiSelection,
							String whereClause)
	{
		super(frame, modal, WindowNo, "ch", "hrm_order_ID", multiSelection, whereClause);
		
		log.info("InfoHRMOrder");
		setTitle(Msg.getMsg(Env.getCtx(), "InfoHRMOrder"));
		try
		{
			statInit();
			p_loadedOK = initInfo ();
			orderType = Env.getContext(Env.getCtx(), p_WindowNo, "hrm_ordertype");
		}
		catch (Exception e)
		{
			return;
		}
		int no = p_table.getRowCount();
		setStatusLine(Integer.toString(no) + " " + Msg.getMsg(Env.getCtx(), "SearchRows_EnterQuery"), false);
		setStatusDB(Integer.toString(no));
		if (value != null && value.length() > 0)
		{
			fName.setValue(value);
			executeQuery();
		}
		//
		pack();
		//	Focus
		fOrder_code.requestFocus();	
		// 
	}
	
//  Static Info
	private CLabel lOrder_code = new CLabel(Msg.translate(Env.getCtx(), "hrm_number"));
	private CTextField fOrder_code =new CTextField(10); 
	private CLabel lName = new CLabel(Msg.translate(Env.getCtx(), "Name"));
	private CTextField fName = new CTextField(10);
	private CLabel lDate_code = new CLabel(Msg.translate(Env.getCtx(), "datefrom"));
	private VDate fDate_code = new VDate("datefrom", false, false, true, DisplayType.Date, Msg.translate(Env.getCtx(), "datefrom"));
				
	private static final Info_Column[] hrm_OrderLayout = {
		//hrm_Order_ID field
		new Info_Column(Msg.translate(Env.getCtx(), "hrm_order_ID"),
			"ch.hrm_order_ID", IDColumn.class),
		//1-field for order number		
		new Info_Column(Msg.translate(Env.getCtx(),"hrm_number"),
			"ch.hrm_number", String.class),
		//2-field for order name
		new Info_Column(Msg.translate(Env.getCtx(), "Name"),
			"ch.Name", String.class),
		//3-field for order date
		new Info_Column(Msg.translate(Env.getCtx(), "Date"),
			"ch.datefrom", Timestamp.class)
	};
	
	/**
	 *	Static Setup - add fields to parameterPanel
	 *  @throws Exception if Lookups cannot be created
	 */
	private void statInit() throws Exception{
		
		lOrder_code.setHorizontalAlignment(JLabel.LEADING);
		lOrder_code.setLabelFor(fOrder_code);
		fOrder_code.setBackground(AdempierePLAF.getInfoBackground());
		fOrder_code.addActionListener(this);
		
		lName.setHorizontalAlignment(JLabel.LEADING);
		lName.setLabelFor(fName);
		fName.setBackground(AdempierePLAF.getInfoBackground());
		fName.addActionListener(this);
		
		lDate_code.setHorizontalAlignment(JLabel.LEADING);
		lDate_code.setLabelFor(fDate_code);
		fDate_code.setBackground(AdempierePLAF.getInfoBackground());

		parameterPanel.setLayout(new ALayout(2,2,true));
		//
		parameterPanel.add(lOrder_code,null);
		parameterPanel.add(fOrder_code,null);
		parameterPanel.add(lName, null);
		parameterPanel.add(fName, null);
		parameterPanel.add(lDate_code, null);
		parameterPanel.add(fDate_code, null);
	
	}
	
	/**
	 *	General Init
	 *	@return true, if success
	 */
	private boolean initInfo (){
		
		//  prepare table
			StringBuffer where = new StringBuffer(" ch.IsActive='Y' "); 
			prepareTable (hrm_OrderLayout,
				"adempiere.hrm_Order ch ",
				where.toString(),
				" ch.hrm_order_ID ");
		return true;
	}

	@Override
	protected String getSQLWhere() {
		
		StringBuffer sql = new StringBuffer();
		if (fName.getText().length() > 0)
			sql.append(" AND UPPER(ch.Name) LIKE ?");
		if (fOrder_code.getText().length() > 0)
			sql.append(" AND UPPER(ch.hrm_number) LIKE ?");
		if (fDate_code.getValue() != null)
		{
			Timestamp from = (Timestamp)fDate_code.getValue();
			if (from != null) 
			sql.append(" AND TO_DATE('01-'||TO_CHAR(ch.DATEFROM,'YYYY-MM'),'DD.YYYY.MM') <= ? " +
					"AND TO_DATE('31-'||TO_CHAR(ch.DATEFROM,'YYYY-MM'),'DD.YYYY.MM') >= ?");
		}
	
		if(!orderType.isEmpty())
			sql.append(" AND ch.hrm_orderType = ?");
		
		return sql.toString();
	}

	@Override
	protected void setParameters(PreparedStatement pstmt, boolean forCount)
			throws SQLException {
		int index = 1;
		if (fName.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fName));
		if (fOrder_code.getText().length() > 0)
			pstmt.setString(index++, getSQLText(fOrder_code));
		//
		if (fDate_code.getValue() != null)
		{
			Timestamp from = (Timestamp)fDate_code.getValue();
			log.fine("Date From=" + from);
			if (from != null)
				pstmt.setTimestamp(index++, from);
			{
				pstmt.setTimestamp(index++, from);
			}
		}
		if(!orderType.isEmpty())
			pstmt.setString(index++, orderType);

	}
	
	/**
	 *  Get SQL WHERE parameter
	 *  @param f field
	 *  @return Upper case text with % at the end
	 */
	
	private String getSQLText(CTextField f) {
		String s = f.getText().toUpperCase();
		if (!s.endsWith("%"))
			s = "%"+s+"%";
		log.fine( "String=" + s);
		return s;
	}

}
